package com.vouov.apps.controllers;

import com.vouov.apps.bean.JsonResponse;
import com.vouov.apps.bean.PagingNavigator;
import com.vouov.apps.bean.User;
import com.vouov.apps.service.interfaces.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * User: yuminglong
 * Date: 11-10-26
 * Time: 下午10:53
 * Version: 1.0.0
 */
@Controller
@RequestMapping("/user/")
public class UserController{
    @Autowired
    IUserService userService;
    @Autowired
    JsonResponse jsonResponse;

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse edit(User user) throws Exception {
        long id = user.getId();
        String action = null;
        if(id>0){
            userService.updateUsers(user);
            action = "update";
        } else {
            userService.addUser(user);
            action = "add";
        }
       // User user_1 = userService.select(user.getId());
        //json.put(MyConst.DATA, user_1);
        return jsonResponse;
    }
    //@RequestMapping("/edit/{id}")
    public String add(@PathVariable long id, Model model) throws Exception {
        User user=null;
        if(id>0){
          // user = userService.select(id);
           if(user==null){
              return "error";
           }
        }
        if(user==null){
           user = new User();
        }
        model.addAttribute("user", user);
        return  "user_edit";
    }
    //@RequestMapping(value = "/search", method = RequestMethod.GET)
    //@ResponseBody
    public HashMap<String,Object> search(PagingNavigator navigator, User user){
           return null;
    }
}
